/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.lehe.common.exceptions.LHAdminException;
import com.lehe.common.exceptions.LHException;

@ControllerAdvice
public class GlobalExceptionHandler {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@ExceptionHandler(value=Exception.class)
	public String errorHandler(Exception e,HttpServletRequest req,HttpServletResponse response){
		
		if(e instanceof LHException){
			logger.error("业务异常",e);
			req.setAttribute("error", e.getMessage());
		}else if(e instanceof LHAdminException){
			if(LHAdminException.CODE_NOT_LOGIN.equals(((LHAdminException) e).getErrorCode())){
				logger.error("未登录");
				return "admin/login/loginJumper";
			}
			req.setAttribute("error", e.getMessage());
			return "exception/admin/error";
		}else {
			logger.error("未知错误",e);
			req.setAttribute("error", "系统繁忙，请关闭页面重试！");
		}
		if(req.getRequestURI().indexOf("admin")>=0){
			return "/exception/admin/error";
		}else {
			return "/exception/error";
		}
	}
}

